package com.lemon.smartsafety.app;

import android.app.Activity;

import java.util.Stack;

/**
 * activity堆栈式管理
 * Created by Cooliean on 15/7/5.
 */
public class AppManager {
    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单例模式
     */
    public static  AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public synchronized void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public synchronized Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }
    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public synchronized void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public synchronized void finishActivity(Activity activity) {
        try {
            if (activity != null && !activity.isFinishing()) {
                activityStack.remove(activity);
                activity.finish();
                activity = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 结束指定类名的Activity
     */
    public synchronized void finishActivity(Class<?> cls) {
//        loggerHelper.w(activityStack.toString());
        for (Activity activity : activityStack) {
            if (activity != null && activity.getClass().equals(cls)) {
                activityStack.remove(activity);
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public synchronized void finishActivityNotRemove(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 结束指定的Activity
     */
    public synchronized void finishActivityNotRemove(Activity activity) {
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
            activity = null;
        }
    }
    /**
     * 结束所有Activity
     */
    public synchronized void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); activityStack !=null && i < size; i++) {
            if (null != activityStack.get(i)) {
                finishActivityNotRemove(activityStack.get(i));
            }
        }
        activityStack.clear();
    }


    /**
     * 获取指定的Activity
     */
    public synchronized static Activity getActivity(Class<?> cls) {
        Activity returnActivity = null;
        if (activityStack != null) {
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) {
                    returnActivity =  activity;
                    break;
                }
            }
        }
        return returnActivity;
    }

    /**
     * 判断指定的Activity是否存在
     */
    public static boolean activityIsExist(Class<?> cls) {
        if (getActivity(cls) != null) {
            return true;
        }else{
            return false;
        }
    }


    /**
     * 退出应用程序
     */
    public void AppExit() {
        try {
            finishAllActivity();
            // 杀死该应用进程
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
